package org.domeos.framework.api.controller.deployment;

import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang.StringUtils;
import org.domeos.basemodel.HttpResponseTemp;
import org.domeos.basemodel.ResultStat;
import org.domeos.framework.api.consolemodel.deployment.DeployCollectionDraft;
import org.domeos.framework.api.controller.common.BaseSreController;
import org.domeos.framework.api.service.deployment.DeployCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 对接SRE接口
 * @author haqiaolong
 *
 */
@Controller
@RequestMapping("/sre/api/deploycollection")
@Api("SRE部署相关API")
public class DeployCollectionSreController extends BaseSreController {
	
	@Autowired
    DeployCollectionService deployCollectionService;
	
	@ApiOperation(value="服务检查")
    @ResponseBody
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public HttpResponseTemp<?> checkDeployCollection(HttpServletRequest request , @RequestBody DeployCollectionDraft deployCollectionDraft) throws Exception {
		if(StringUtils.isEmpty(deployCollectionDraft.getName())){
			return ResultStat.PARAM_ERROR.wrap("service name is null!");
		}
		String userId = getUserId(request);
		if(StringUtils.isEmpty(userId)){
			return ResultStat.PARAM_ERROR.wrap("service userId is null!");
		}
		deployCollectionDraft.setUserId(getIntValue(userId));
        return deployCollectionService.checkDeployCollection(deployCollectionDraft);
    }
	
	@ApiOperation(value="服务创建")
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public HttpResponseTemp<?> createDeployCollection(HttpServletRequest request , @RequestBody DeployCollectionDraft deployCollectionDraft) throws Exception {
		if(StringUtils.isEmpty(deployCollectionDraft.getName())){
			return ResultStat.PARAM_ERROR.wrap("service name is null!");
		}
		String userId = getUserId(request);
		if(StringUtils.isEmpty(userId)){
			return ResultStat.PARAM_ERROR.wrap("service userId is null!");
		}
		deployCollectionDraft.setUserId(getIntValue(userId));
        return deployCollectionService.sreCreateDeployCollection(deployCollectionDraft);
    }
}
